package com.etgraph.recap.text;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.etgraph.recap.msof.template.griditem.RecapItem;

public class RecapTextParserImpl implements RecapTextParser {
	private static final String matchDateRegex = "\\[[0-2]*[0-9]:[0-6][0-9][- ][A|P]M\\]:";
	private static final String matchGroupRegex = "\\(ADEV\\-DEV\\-CS\\/ZHA\\)";
	private static final String KEY_WORD_DOWN_COMPLEX = "(D|d)(O|o)(W|o)(N|n)";
	private static final String KEY_WORD_DOWN_SIMPLE = "(D|d)own";
	private static final String KEY_WORD_FOLLOW_COMPLEX = "(F|f)(O|)(L|l)(L|l)(O|o)(W|w)";
	private static final String KEY_WORD_FOLLOW_SIMPLE = "(F|f)ollow";
	private static final String KEY_WORD_FOLLOW_UP_COMPLEX = "(F|f)(O|)(L|l)(L|l)(O|o)(W|w) (U|u)(P|p)";
	private static final String KEY_WORD_FOLLOW_UP_SIMPLE = "(F|f)ollow up";

	public List<RecapItem> parserText(String text) {
		//WARNING:
		//the space between ')' and '[' is not real space character,
		//it combined with 7 characters,if you still use the space character to match them,
		//it will be failed, the right way to do this is copy the character(s) from Text sources!
		byte [] b = text.getBytes();
		String trimedStr = relpaceAllIllegalCharactor(b);
		String [] reports = trimedStr.split("\n|\r|\r\n");
		getAllRecapItem(reports);
		
		
//		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		bos.toByteArray();
		return null;
	}

	private String relpaceAllIllegalCharactor(byte[] b) {
		for (int i = 0; i < b.length; i++) {
			if(b[i]<0 || b[i]>127){
				b[i] = ' ';
			}
		}
		String conbineStr = new String(b);
		String trimedStr = conbineStr.replaceAll("\t", "");
		trimedStr = trimedStr.replaceAll("?", " ");
		return trimedStr;
	}

	private List<RecapItem> getAllRecapItem(String[] reports) {
		List<RecapItem> items = new ArrayList<RecapItem>();
		List<String> lines = new ArrayList<String>();
		RecapItem item = null;
		for (String report : reports) {
			if(isMappedItem(report)){
				extractText(report);
				item = constructItem(lines);
				if(item != null){
					items.add(item);
				}
			}else{
				lines.add(report);
			}
		}
		return items;
	}

	private RecapItem constructItem(List<String> lines) {
		RecapItem item = null;
		if(!lines.isEmpty()){
			item = new RecapItem();
			for (String line : lines) {
				line = line.trim();
				if(isMappedAuthor(line)){
					String[] authorInfos = line.split(matchGroupRegex);
					if(authorInfos.length>0){
						String author = authorInfos[0].trim();
						item.setName(author);
					}
				}else{
					String[] downContent = mappedKeyWord(KEY_WORD_DOWN_SIMPLE, line);
					String[] followContent = mappedKeyWord(KEY_WORD_FOLLOW_SIMPLE, line);
					String[] followUpContent = mappedKeyWord(KEY_WORD_FOLLOW_SIMPLE, line);
					if(downContent.length>1){
						String downText = reconstructItemConent(downContent, KEY_WORD_DOWN_SIMPLE);
						item.setDown(downText);
					}
					if(followContent.length>1){
						String followText = reconstructItemConent(followContent, KEY_WORD_FOLLOW_SIMPLE);
						item.setFollowUp(followText);
					}else if(followUpContent.length>1){
						String followText = reconstructItemConent(followUpContent, KEY_WORD_FOLLOW_UP_SIMPLE);
						item.setFollowUp(followText);
					}
				}
			}
			lines.clear();
			return item;
		}
		return null;
	}

	private String reconstructItemConent(String[] downContent, String keyword) {
		String downText= "";
		for (String c : downContent) {
			downText = keyword + c;
		}
		return downText;
	}

	private RecapItem extractText(String lineText){
		return null;
	}

	private boolean isMappedAuthor(String title){
		Pattern p = Pattern.compile(matchGroupRegex);
		Matcher m = p.matcher(title);
		return m.matches();
	}
	
	private String[] mappedKeyWord(String key, String word){
		return word.split(key);
	}
	
	private boolean isMappedItem(String lineText){
		Pattern p = Pattern.compile(matchGroupRegex);
		Matcher m = p.matcher(lineText);
		return m.matches();
	}
	
	public void minitrim() {
		// TODO Auto-generated method stub

	}

	public void errorHandler() {
		// TODO Auto-generated method stub

	}

	public void sendMailToUnSubmitUser() {
		// TODO Auto-generated method stub

	}

	public void sendMailToCollector() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		String[] ss = "     ‎‎JASON WU (ADEV-DEV-CS/ZHA)‎‎   [9:40 AM]:    \r\n\t".split(matchGroupRegex);
		String[] sss = "    YassyAsaYA  :  sdfsdf \r\t".split("(y|Y)(a|A)");
		for (String s : sss) {
			System.out.println(s.trim());
		}
	}
}
